package com.fangyi.yunmusic.controller;

import com.fangyi.yunmusic.enums.FileTypeEnum;
import com.fangyi.yunmusic.pojo.Playlist;
import com.fangyi.yunmusic.pojo.vo.PlaylistDetailVO;
import com.fangyi.yunmusic.service.PlaylistService;
import com.fangyi.yunmusic.utils.FileUploadClient;
import com.fangyi.yunmusic.utils.PagedResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class PlaylistController {

    Logger logger = LoggerFactory.getLogger(PlaylistController.class);

    @Autowired
    private PlaylistService playlistService;

    @Autowired
    private FileUploadClient fileUploadClient;

    @PostMapping("/playlist/addPlayCount")
    public Object addPlayCount(String playlistId) {
        return playlistService.addPlayCount(playlistId);
    }

    @PostMapping("/playlist/publish")
    public Object publishPlaylist(Playlist playlist) {
        return playlistService.insertPlaylist(playlist);
    }

    @PostMapping("/playlist/delete")
    public Object deletePlaylist(String playlistId) {
        playlistService.deletePlaylist(playlistId);
        return "删除成功";
    }

    @PostMapping("/playlist/update")
    public Object updatePlaylist(Playlist playlist) {
        playlistService.updatePlaylist(playlist);
        return "更新成功";
    }

    @GetMapping("/top/playlist")
    public Object getPlaylists(Integer pageNum, Integer pageSize, String order) {
        PagedResult pagedResult = playlistService.selectPlaylistVO(pageNum, pageSize, order);
        return pagedResult;
    }

    @GetMapping("/playlist/detail")
    public Object getPlaylistDetail(@RequestParam String id) {
        PlaylistDetailVO playlistDetailVO = playlistService.getPlaylistDetailVO(id);
        return playlistDetailVO;
    }

    @GetMapping("/playlist/userCreated")
    public Object getCreatedPlaylistsByUserId(@RequestParam String id,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "999") Integer pageSize) {
        PagedResult pagedResult = playlistService.selectCreatedPlayListByUserId(id, pageNum, pageSize);
        return pagedResult;
    }

    @GetMapping("/playlist/userSubscribed")
    public Object getSubscribePlaylistsByUserId(@RequestParam String id,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "999") Integer pageSize) {
        PagedResult pagedResult = playlistService.selectSubscribedPlayListByUserId(id, pageNum, pageSize);
        return pagedResult;
    }

    @GetMapping("/playlist/count")
    public Object getPlaylistCount(){
        return playlistService.getPlaylistCount();
    }
    @PostMapping("/playlist/addSong")
    public Object addSongForPlaylist(@RequestParam String userId,
                                     @RequestParam String songId,
                                     @RequestParam String playlistId) {

        playlistService.addSongForPlaylist(userId, playlistId, songId);
        return "添加成功";
    }

    @PostMapping("/playlist/deleteSong")
    public Object deleteSongForPlaylist(@RequestParam String userId,
                                        @RequestParam String songId,
                                        @RequestParam String playlistId) {

        playlistService.deleteSongForPlaylist(userId, playlistId, songId);
        return "删除成功";
    }

    /**
     * 上传成功，则返回文件路径，以及文件ID
     * @param file
     * @return
     */
    @PostMapping("/playlist/uploadCover")
    public Object uploadCover(@RequestParam MultipartFile file){
        FileUploadClient.FileMessageResponse response = fileUploadClient.uploadToFarService(file, FileTypeEnum.PLAYLIST_COVER);
        return response;
    }



}
